package com.dut.bbs.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.dut.bbs.R;
import com.dut.bbs.entity.LoginEntity;
import com.dut.bbs.util.Dg;

public class LoginActivity extends BaseActivity {
	private EditText et_pwd,et_uname;
	private Button bt_ok;
	private Context context;
	private SharedPreferences settings; 
	private CheckBox cb_autolog,cb_rempwd;
    private Handler handler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {            
            if(msg.what==1){
            	Intent intent = getIntent();
            	String flag = intent.getStringExtra("fromItem");
            	if(!"1".equals(flag))
            	{
            		Intent in = new Intent(context, MainActivity.class);
                	startActivity(in);
            	}
    			LoginActivity.this.finish(); 
            }else if(msg.what==2)
            	Dg.showToast(context, "用户名或密码错误！");
            else if(msg.what ==3)
            	Dg.showToast(context, "用户名不能为空！");
            else if(msg.what==4)
            	Dg.showToast(context, "密码不能为空！");
            else
            	Dg.showToast(context, "亲，你的网络不给力哦！");
            getProgressDialog().dismiss();  //进度条消失  
        }  
    };  
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		context=LoginActivity.this;
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		TextView tv_title = (TextView) findViewById(R.id.activitytitle);
		tv_title.setText("碧海青天BBS");
		et_uname = (EditText)findViewById(R.id.UserNameEditText);
		et_pwd = (EditText)findViewById(R.id.PasswordEditText);
		bt_ok = (Button)findViewById(R.id.LoginButton);
		cb_autolog = (CheckBox) findViewById(R.id.automaticlogin);
		cb_rempwd = (CheckBox) findViewById(R.id.rememberpassword);
		cb_autolog.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if(cb_autolog.isChecked()){
					if(!cb_rempwd.isChecked())
						cb_rempwd.setChecked(true);
				}
			}
			
		});
		cb_rempwd.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(!cb_rempwd.isChecked()){
					if(cb_autolog.isChecked()){
						cb_autolog.setChecked(false);
					}
				}
			}			
		});
		bt_ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getProgressDialog().setMessage("正在登录...");
				getProgressDialog().show();  
				Runnable r = new ConnectThread();
				new Thread(r).start();
				} 
			});
		findViewById(R.id.AnonyButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent in = new Intent(context, MainActivity.class);
            	startActivity(in);
            	getCurrentActivity().finish();
			}
		});
		if(settings.getInt("autologin", 0)==1){
			cb_autolog.setChecked(true);
			String name_store=settings.getString("name","");
            String pwd_store=settings.getString("password", "");
            et_uname.setText(name_store);
            et_pwd.setText(pwd_store);
            getProgressDialog().setMessage("正在登录...");
            getProgressDialog().show();
            Runnable r = new ConnectThread();
			new Thread(r).start();
//            try {
//				if(LoginEntity.login(name_store, pwd_store)){
////					Dg.showToast(LoginActivity.this, "登录成功");
//				Intent in = new Intent(context, PostActivity.class);
//				startActivity(in);
//				LoginActivity.this.finish();
//				}else
//					Dg.showDg(context, "用户名或密码错误！");
//			} catch (Exception e) {
//				Dg.showToast(context, "亲，你的网络不给力哦！");
//			}		
		}else if(settings.getInt("rempwd", 0)==1){
			String name_store=settings.getString("name","");
            String pwd_store=settings.getString("password", "");
            et_uname.setText(name_store);
            et_pwd.setText(pwd_store);
		}
	}
	class ConnectThread implements Runnable{

		@Override
		public void run() {
			int message =0;
			String uname = et_uname.getText().toString();
			String pwd = et_pwd.getText().toString();
			if (uname == null || uname.length() <= 0)
				message = 3;
			else if (pwd == null || pwd.length() <= 0)
				// Dg.showDg(context, "密码不能为空！");
				message = 4;
			else {
				try {
					if (LoginEntity.login(uname, pwd)) {
						// Dg.showToast(LoginActivity.this, "登录成功");
						SharedPreferences.Editor editor = settings.edit();
						if (cb_rempwd.isChecked()) {
							editor.putString("name", uname);
							editor.putString("password", pwd);
							editor.putInt("rempwd", 1);
							if (cb_autolog.isChecked()) {
								editor.putInt("autologin", 1);
							}
							editor.commit();
						}
						message = 1;
					} else {
						message = 2;
						SharedPreferences.Editor editor = settings.edit();
						editor.clear();
						editor.commit();
					}
				} catch (Exception e) {
					Log.v("LoginActivity", e.toString());
				}
			}
			handler.sendEmptyMessage(message);
		}
	}
}
